package com.kuaimacode.kframework.admin.controller.category;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.category.AgentServService;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.category.AgentServ;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.Subject;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/03/22
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/agentService"})
public class AgentServiceController {
	private static String PAGE_FILE_NAME = "agentService/";

	@Autowired
	private AgentServService agentServiceServices;
	@Autowired
	private DictService dictService;


	@RequestMapping(value = {"/index/{code}"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						@PathVariable String code,ModelMap model) {
		PageInfo<AgentServ> pageInfo = agentServiceServices.findAll(pageNum, pageSize,code);
		// pageInfo.list agentService
		model.put("pageInfo", pageInfo);
		model.put("serviceClass", code);
		model.put("serviceClassName", DictUtil.keyValue(Constant.SERVICE_CLASS_DICT_NAME,code));
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add/{serviceClass}", method = RequestMethod.GET)
	public String add(@PathVariable String serviceClass,ModelMap map) {
		AgentServ agentService = new AgentServ();
		agentService.setServiceClass(serviceClass);
		map.put("agentService", agentService);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}



	@RequestMapping(value = "/addOneClass", method = RequestMethod.GET)
	public String addOneClass(ModelMap map) {
		Dict dict = new Dict();
		map.put("dict", dict);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"oneClassform";
	}





	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		AgentServ agentService = agentServiceServices.getById(id);
		map.put("agentService", agentService);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(AgentServ agentService,Boolean isAdd,String updateAll,  ModelMap map) {
		try {
			String retMsg ="";
			if(isAdd){
				// 新建保存
				retMsg = agentServiceServices.saveService(agentService);
			}else{
				// 编辑保存
				retMsg = agentServiceServices.updateService(agentService);
			}
			if(!"success".equals(retMsg)) return JsonResult.failure(retMsg);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/changeStat/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id,@RequestParam(value = "status", required = true)String status, ModelMap map) {
		try {
			AgentServ agentServ = agentServiceServices.getById(id);
			agentServ.setDeleteStatus(Integer.parseInt(status));
			agentServiceServices.updateById(agentServ);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = {"/getChildType/{parentCode}"}, method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getChildTypeList(@PathVariable String parentCode) {
		Map<String,String> map =DictUtil.keyValueByPCodeList(Constant.SERVICE_CLASS_DICT_NAME,parentCode);
		return JsonResult.success(map);
	}


}
